#!/usr/bin/perl -w 
#cabecera
use UUT;
use Validator::Logger;
use Test;

sub isanumber{
    if($_[0]=~/^[+-]?\d+$/){
	return(1);
    }else{
	return(0);
    }
}
my $model=substr($0,rindex($0,"/")+1,(index($0,".pl")-(rindex($0,"/")+1)));
use constant MODEL_ID=>1252;
use constant INIT_TIMEOUT=>90;
use constant ETHLAN_PORT_N=>4;

%indice=("-sn"=>-1,"-en"=>-1,"-sh"=>-1);
#lectura de parametros
$i=-1;
foreach $param(@ARGV){
    $i++;
    if ($i%2==0){
        if($param eq "-help"){print "\nusage: $0 -en environment -sh shelf -sn serial_number\n";exit(1);}
        elsif($param eq "-sn"){$indice{"-sn"}=$i+1;}
        elsif($param eq "-en"){$indice{"-en"}=$i+1;}
        elsif($param eq "-sh"){$indice{"-sh"}=$i+1;}
        else {die("\nSINTAX ERROR: Unknown parameter: \"$param\". Help: $0 -help");}
    }
}
if($indice{"-sn"}==-1){
	die("\nSINTAX ERROR: Serial number parameter (-sn) not given.");
}
if($indice{"-en"}==-1){
	die("\nSINTAX ERROR: Environment parameter (-en) not given.");
}
if (($indice{"-sh"}==-1)||(!isanumber($ARGV[$indice{"-sh"}]))){
	die("\nSINTAX ERROR: Shelf number parameter (-sh) not given or it is not a number.");
}

my $environment=$ARGV[$indice{"-en"}];
my $shelf=$ARGV[$indice{"-sh"}];
my $serial_number=$ARGV[$indice{"-sn"}];
    
#parametros
#$environment='DOCSIS1';
#$serial_number="218134250";
#$shelf=1;

my $firmware="epc2325-v202r1289-111014cs";

my $test=new Test(model=>$model,serial_number=>$serial_number,model_id=>MODEL_ID,ethlan_port_n=>ETHLAN_PORT_N,environment=>$environment,shelf=>$shelf,intranet_url=>'http://intranet.turyelectro.com/intranet/xmlrpc/xmlrpc.asp',intranet_user=>'ruben',intranet_password=>'router00');
my $uut=$test->{uut};
my $logger=$test->{logger};
#RESET INICIAL Y PROVISION
$uut->provision();
if(!$uut->factoryDefaults(configuration_ip=>'MANUAL')){
	if($uut->testPowerVsStart()){
		$test->failed(subtest=>"START",message=>"CANNOT START UUT");
	}else{
		$test->failed(subtest=>"POWER",message=>"CANNOT POWER UUT");
	}
}
$uut->checkQualityCableWAN_init();
#primer arranque
my $retries=3;
INIT_TEST:$uut->waitForInitialization(wait_ip=>$uut->get('cablewan_ip'),timeout=>INIT_TIMEOUT) || $test->failed(subtest=>"CABLEWAN",message=>"CANNOT START UUT OR CABLE FAILS");
sleep(2);
print $uut->getBootState(model=>'GENERIC-CABLEMODEM');
if($uut->getBootState(model=>'GENERIC-CABLEMODEM')!=1){$retries--;if($retries>0){sleep(5);goto INIT_TEST;}$test->failed(subtest=>"CABLEWAN",message=>"CANNOT INITIALIZE CABLEMODEM COMPLETELY");}
my $firmwareOperStatus=$uut->getFirmwareOperStatus(model=>'GENERIC-CABLEMODEM');
if(!$firmwareOperStatus){$retries--;if($retries>0){sleep(5);goto INIT_TEST;}$test->failed(subtest=>"CABLEWAN",message=>"CANNOT GET FIRMWARE OPERATIONAL STATUS");}
FIRM_DOWNLOAD:while($firmwareOperStatus==1){
		$firmwareOperStatus=$uut->getFirmwareOperStatus(model=>'GENERIC-CABLEMODEM');
		sleep(1);
}
if($firmwareOperStatus==4){$test->failed(subtest=>"CABLEWAN","CANNOT LOAD FIRMWARE");}
sleep(10); #para evitar rebotes
#despues de cargar el firmware, se resetea
$uut->waitForInitialization(wait_ip=>$uut->get('cablewan_ip'),timeout=>INIT_TIMEOUT) || $test->failed(subtest=>"CABLEWAN",message=>"CANNOT START UUT OR CABLE FAILS AFTER FIRMWARE UPGRADE");
$firmwareOperStatus=$uut->getFirmwareOperStatus(model=>'GENERIC-CABLEMODEM')|| $test->failed(subtest=>"CABLEWAN",message=>"CANNOT GET FIRMWARE OPERATIONAL STATUS AFTER FIRMWARE UPGRADE");
#if(($firmwareOperStatus==4)||($firmwareOperStatus==5)){if($firmwareOperStatus==5){goto FIRM_DOWNLOAD;}$test->failed(subtest=>"CABLEWAN",message=>"CANNOT LOAD FIRMWARE AFTER FIRMWARE UPGRADE");}
if($firmwareOperStatus==4){$test->failed(subtest=>"CABLEWAN",message=>"CANNOT LOAD FIRMWARE AFTER FIRMWARE UPGRADE");}
if($firmwareOperStatus==1){print "RELOADING FIRMWARE";goto FIRM_DOWNLOAD;}
if($firmware ne $uut->getFirmware(model=>'GENERIC-CABLEMODEM')){$test->failed(subtest=>"CABLEWAN",message=>"NEW FIRMWARE NOT LOADED AFTER FIRMWARE UPGRADE")}
#CONFIGURACION
$uut->set('ethlan_ip'=>"192.168.0.109");
$uut->set('wifilan_ip'=>"192.168.0.109");
$uut->configureWifiLAN(wifilan_channel=>1,configuration_ip=>$uut->get('cablewan_ip'))||$test->failed(subtest=>"SOFTWARE",message=>"CANNOT CONFIGURE WIFILAN / ".$uut->lastErrorMsg());
$uut->testCableWAN()||$test->failed(subtest=>"CABLEWAN",message=>"TRAFFIC TEST FAILED / ".$uut->lastErrorMsg());
$uut->configureEthLAN(configuration_ip=>$uut->get('cablewan_ip'))||$test->failed(subtest=>"SOFTWARE",message=>"CANNOT CONFIGURE ETHLAN / ".$uut->lastErrorMsg());
#se reinicia
sleep(10);
#PRUEBA
$uut->waitForInitialization(wait_ip=>$uut->get('cablewan_ip'),timeout=>INIT_TIMEOUT) || $test->failed(subtest=>"CABLEWAN",message=>"CANNOT START UUT OR CABLE FAILS AFTER CONFIGURATION");
my $okEthPorts=$uut->testEthLAN();
if($okEthPorts < ETHLAN_PORT_N){$test->failed(subtest=>"ETHLAN",message=>"TRAFFIC TEST FAILED - PORT ".($okEthPorts+1)." / ".$uut->lastErrorMsg());}

$uut->blockWifiLAN();
$uut->configureWifiLAN(configuration_ip=>$uut->get('cablewan_ip'))||$test->failed(subtest=>"SOFTWARE",message=>"CANNOT CONFIGURE WIFILAN".$uut->lastErrorMsg());;
$uut->testWifiLAN()||do{my $lasterror=$uut->lastErrorMsg();$uut->configureWifiLAN(wifilan_channel=>1,configuration_ip=>$uut->get('cablewan_ip'));$test->failed(subtest=>"WIFILAN",message=>"TRAFFIC TEST FAILED / ".$lasterror);};
$uut->configureWifiLAN(wifilan_channel=>1,configuration_ip=>$uut->get('cablewan_ip'))||$test->failed(subtest=>"SOFTWARE",message=>"CANNOT CONFIGURE WIFILAN".$uut->lastErrorMsg());
$uut->unblockWifiLAN();

if($uut->get('customer_id')==5){
		$uut->checkQualityCableWAN()||$test->failed(subtest=>"CABLEWAN",message=>"QUALITY PARAMETERS DO NOT MEET REQUIREMENTS / ".$uut->lastErrorMsg());
}
#RESET A FABRICA
$uut->factoryDefaults(configuration_ip=>$uut->get('cablewan_ip'))||$test->failed(subtest=>"SOFTWARE",message=>"FINAL RESET TO FACTORY DEFAULTS FAILED / ".$uut->lastErrorMsg());

$test->succeed();

